<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="org.apache.catalina.Session"%>
<%@ page import="java.sql.Date,java.sql.*"%>
<%@ page import="cn.ticket.db.DBConnect"%>
<%@ page import="cn.ticket.util.StringUtil"%>
<%@ page import="cn.ticket.Constant"%>
    <%
    request.setCharacterEncoding("utf8" ) ;
    String busId= request.getParameter("busId") ;
	String ticketId = request.getParameter("id") ;
	String userId= (String)session.getAttribute("login_user_id") ;
	
	if(null==busId  || "".equals(busId)){
		session.setAttribute("errorinfo", Constant.ERRORINFO_PARAMETERERROR) ;
		%>
		<jsp:forward page="/front/result.jsp" />
		<%
	}		if(null==ticketId  || "".equals(ticketId)){
		session.setAttribute("errorinfo", Constant.ERRORINFO_PARAMETERERROR) ;
		%>
		<jsp:forward page="/front/result.jsp" />
		<%
	}		
	Connection con = new DBConnect().DBConnection() ;
	try {
		String sql="update ticket set status="+Constant.TICKET_UPDATE_SUCCESS+" where id=?" ;
		PreparedStatement pstmt=con.prepareStatement(sql) ;
		pstmt.setString(1,ticketId) ;
		pstmt.executeUpdate() ;
		%>
		<jsp:forward page="/front/ticket/orderList.jsp?result=success" />
		<%
	}catch(Exception e){
		session.setAttribute("errorinfo", Constant.ERRORINFO_NETERROR+e) ;		
		%>
		<jsp:forward page="/front/result.jsp?result=系统异常了,请稍后再操作." />
		<%
	}
	String sql="select * from bus where id=?" ;
	PreparedStatement pst = con.prepareStatement(sql) ;	
	pst.setString(1,busId) ;
	ResultSet rs = pst.executeQuery() ;
	if(rs.next()){
		sql = "insert into ticket(id,user_id,oriStation,desStation,time,creTime,status,num,price) values(?,?,?,?,?,?,?,?,?)" ;
		PreparedStatement pstmts = con.prepareStatement(sql) ;
		String id = StringUtil.createUUID() ;
		pstmts.setString(1, id) ;
		pstmts.setString(2, userId ); 
		pstmts.setString(3,rs.getString(3)) ;
		pstmts.setString(4, rs.getString(4)) ;
		pstmts.setString(5,rs.getString(6) ) ;
		pstmts.setDate(6, new java.sql.Date(new java.util.Date().getTime())) ;
		pstmts.setInt(7, 0) ;
		pstmts.setString(8, rs.getString(2)) ;
		pstmts.setInt(9, rs.getInt(5)) ;
		pstmts.executeUpdate() ;
		%>
		<jsp:forward page="orderList.jsp"></jsp:forward>
	<%
	}
%>
